package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TbMemberCard;
import com.ruoyi.system.domain.TbMemberRecords;
import com.ruoyi.system.domain.TbUserInfo;
import com.ruoyi.system.domain.request.MemberRequest;
import com.ruoyi.system.domain.vo.TbMemberVO;
import com.ruoyi.system.domain.vo.TbMemberViewVO;
import com.ruoyi.system.service.ITbMemberCardService;
import com.ruoyi.system.service.ITbMemberRecordsService;
import com.ruoyi.system.service.ITbUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbMemberMapper;
import com.ruoyi.system.domain.TbMember;
import com.ruoyi.system.service.ITbMemberService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 会员管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-18
 */
@Service
public class TbMemberServiceImpl implements ITbMemberService 
{
    @Autowired
    private TbMemberMapper tbMemberMapper;

    @Resource
    private ITbUserInfoService userInfoService;

    @Resource
    private ITbMemberCardService memberCardService;

    @Resource
    private ITbMemberRecordsService recordsService;

    /**
     * 查询会员管理
     * 
     * @param id 会员管理主键
     * @return 会员管理
     */
    @Override
    public TbMember selectTbMemberById(Long id)
    {
        return tbMemberMapper.selectTbMemberById(id);
    }

    /**
     * 查询会员管理列表
     * 
     * @param tbMember 会员管理
     * @return 会员管理
     */
    @Override
    public List<TbMemberVO> selectTbMemberList(MemberRequest tbMember)
    {
//        return tbMemberMapper.selectTbMemberList(tbMember);
        return tbMemberMapper.selectPageList(tbMember);
    }

    /**
     * 新增会员管理
     * 
     * @param tbMember 会员管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTbMember(TbMember tbMember)
    {
        TbMember tbMemberUser =  tbMemberMapper.findUserId(tbMember.getUserId());
        if (!ObjectUtils.isEmpty(tbMemberUser)){
            throw new RuntimeException("该会员{"+tbMember.getMemberName()+"}已存在，会员卡已绑定，请勿重复添加！");
        }
        tbMember.setCreateTime(DateUtils.getNowDate());
        TbUserInfo tbUserInfo = userInfoService.selectTbUserInfoById(tbMember.getUserId());
        tbMember.setMemberName(tbUserInfo.getUserName());
        TbMemberCard tbMemberCard = memberCardService.selectTbMemberCardById(tbMember.getMemberCradId());
        BeanUtils.copyProperties(tbMemberCard,tbMember);
        String desc= tbMember.getMemberName()+"会员购买了："+ tbMemberCard.getMemberCardName()+"：会员卡,并且建立了数据库专属数据！";
        tbMemberMapper.insertTbMember(tbMember);
        addRecord(tbMember, tbMemberCard,desc);
        return 1;
    }

    private void addRecord(TbMember tbMember, TbMemberCard tbMemberCard,String desc) {
        TbMemberRecords records = new TbMemberRecords();
        records.setMemberId(tbMember.getId());
        records.setMemberType(4L);
        records.setMemberName(tbMember.getMemberName());
        records.setRemark(desc);
        records.setCreateTime(new Date());
        recordsService.insertTbMemberRecords(records);
    }

    /**
     * 修改会员管理
     * 
     * @param tbMember 会员管理
     * @return 结果
     */
    @Override
    public int updateTbMember(TbMember tbMember)
    {
        tbMember.setUpdateTime(DateUtils.getNowDate());
        TbMember tbMember1 = tbMemberMapper.selectTbMemberById(tbMember.getId());
        if (!tbMember1.getMemberCradId().equals(tbMember.getMemberCradId())){
            TbMemberCard tbMemberCard = memberCardService.selectTbMemberCardById(tbMember.getMemberCradId());
            BeanUtils.copyProperties(tbMemberCard,tbMember);
            String desc= tbMember.getMemberName()+"会员购买了："+ tbMemberCard.getMemberCardName()+"：会员卡,并重新绑定，初始化权益次数！";
            addRecord(tbMember, tbMemberCard,desc);
        }
        return tbMemberMapper.updateTbMember(tbMember);
    }

    /**
     * 批量删除会员管理
     * 
     * @param ids 需要删除的会员管理主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberByIds(Long[] ids)
    {
        return tbMemberMapper.deleteTbMemberByIds(ids);
    }

    /**
     * 删除会员管理信息
     * 
     * @param id 会员管理主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberById(Long id)
    {
        return tbMemberMapper.deleteTbMemberById(id);
    }

    @Override
    public TbMemberViewVO getById(Long id) {
        return tbMemberMapper.getById(id);
    }

    @Override
    public List<TbMemberVO> getMemberList() {
        return tbMemberMapper.getMemberList();
    }
}
